package com.wish.g04.financial.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.dto.JrFactoringDTO;
import com.wish.g04.base.entity.CfContract;
import com.wish.g04.base.entity.CwReceiptPlan;
import com.wish.g04.base.entity.JrFactoring;
import com.wish.g04.base.entity.JrProduct;
import com.wish.g04.base.service.ICfContractService;
import com.wish.g04.base.service.ICwReceiptPlanService;
import com.wish.g04.base.service.IJrFactoringService;
import com.wish.g04.base.service.IJrProductService;
import com.wish.g04.base.vo.FactoringDetailVO;
import com.wish.g04.common.core.utils.poi.ExcelUtil;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.financial.service.IFactoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g04.common.log.annotation.Log;
import com.wish.g04.common.log.enums.BusinessType;
import com.wish.g04.common.security.annotation.PreAuthorize;


/**
 * 租约保理申请Controller
 *
 * @author Z-7
 * @date 2022-05-28
 */
@RestController
@RequestMapping("/factoring")
public class JrFactoringController extends BaseController
{
    // 金融产品
    @Autowired
    private IJrProductService iJrProductService;
    // 合同
    @Autowired
    private ICfContractService iCfContractService;
    // 租约保理
    @Autowired
    private IFactoringService iFactoringService;
    // 收款计划
    @Autowired
    private ICwReceiptPlanService iCwReceiptPlanService;

    /**
     * 查询租约保理申请列表
     */
    @PreAuthorize(hasPermi = "financial:factoring:list")
    @GetMapping("/list")
    public TableDataInfo list(JrFactoring jrFactoring)
    {
        startPage();
        List<JrFactoring> list = iFactoringService.list();
        return getDataTable(list);
    }


    /**
     * 获取租约保理申请详细信息
     */
    @PreAuthorize(hasPermi = "financial:factoring:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.error();
    }

    /**
     * 新增租约保理申请
     */
    @PreAuthorize(hasPermi = "financial:factoring:add")
    @Log(title = "租约保理申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JrFactoringDTO jrFactoringDTO)
    {
        // 申请租约保理
        boolean result = iFactoringService.addJrFactoring(jrFactoringDTO);
        if (result) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改租约保理申请
     */
    @PreAuthorize(hasPermi = "financial:factoring:edit")
    @Log(title = "租约保理申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JrFactoring jrFactoring)
    {
        return AjaxResult.error();
    }

    /**
     * 删除租约保理申请
     */
    @PreAuthorize(hasPermi = "financial:factoring:remove")
    @Log(title = "租约保理申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return AjaxResult.error();
    }

    /**
     * 查询
     * 产品类型为'应收租约保理' 并且没有被删除的 金融产品
     */
    @GetMapping("getProduct")
    public AjaxResult getProduct(){
        QueryWrapper<JrProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("jr_product_type",3).eq("del_flg",0);
        List<JrProduct> list = iJrProductService.list(wrapper);
        return AjaxResult.success(list);
    }

    /**
     * 获取租房合同
     * @param jrFactoringDTO
     * @return
     */
    @GetMapping("getContract")
    public TableDataInfo getContract(JrFactoringDTO jrFactoringDTO) {
        startPage();
        List<CfContract> list =  iCfContractService.getList(jrFactoringDTO);
        return getDataTable(list);
    }

    /**
     * 获取选中的租房合同
     * @param jrFactoringDTO
     * @return
     */
    @GetMapping("/getSelectContract")
    public AjaxResult getSelectContract(JrFactoringDTO jrFactoringDTO) {
        List<FactoringDetailVO> factoringDetailVOS = iCwReceiptPlanService.selectByContractId(jrFactoringDTO);
        List<FactoringDetailVO> list = new ArrayList<>();
        for (FactoringDetailVO factoringDetailVO : factoringDetailVOS) {
            factoringDetailVO = iCfContractService.setMessage(factoringDetailVO);
            list.add(factoringDetailVO);
        }
        AjaxResult success = AjaxResult.success(list);
        return success;
    }
}
